package com.dao.good;

import com.dao.BaseDao;
import com.mysql.cj.util.StringUtils;
import com.pojo.Apply;
import com.pojo.Good;
import com.pojo.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class GoodDaoImpl implements GoodDao{

    @Override
    public List<Object> getSearchGoodList(Connection connection, String goodName) throws SQLException {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<Object> goods = new ArrayList<>();
        List<Object> list = new ArrayList<>();

        if(connection != null){
            StringBuffer sql = new StringBuffer();
            sql.append("select * from user,good WHERE user.userId=good.userId");
            if(!StringUtils.isNullOrEmpty(goodName)){
                sql.append(" and goodName like ?");
                list.add("%"+goodName+"%");
            }
            Object[] o = list.toArray();
            System.out.println("sql : "+sql.toString());
            resultSet = BaseDao.excute(connection,preparedStatement,resultSet,sql.toString(),o);
            while (resultSet.next()){
                Good good = new Good();
                good.setGoodId(resultSet.getInt("goodId"));
                good.setUserId(resultSet.getInt("userId"));
                good.setGoodName(resultSet.getString("goodName"));
                good.setGoodAddress(resultSet.getString("goodAddress"));
                good.setGoodExplain(resultSet.getString("goodExplain"));
                User user = new User();
                user.setUserName(resultSet.getString("userName"));
                user.setUserQq(resultSet.getString("userQQ"));
                user.setUserAddress(resultSet.getString("userAddress"));
                user.setUserPhone(resultSet.getString("userPhone"));
                good.setUser(user);
                goods.add(good);

            }
            System.out.println("Dao中："+goods.toString());
            BaseDao.closeResource(null,preparedStatement,resultSet);
        }
        return goods;
    }

    @Override
    public List<Object> getUserGoodList(Connection connection, int userId) throws SQLException {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<Object> goods = new ArrayList<>();
        String sql = "select * from good where userId = ?";
        Object[] o = {userId};
        resultSet = BaseDao.excute(connection, preparedStatement, resultSet, sql, o);
        while (resultSet.next()) {
            Good good = new Good();
            good.setGoodId(resultSet.getInt("goodId"));
            good.setUserId(resultSet.getInt("userId"));
            good.setGoodName(resultSet.getString("goodName"));
            good.setGoodExplain(resultSet.getString("goodExplain"));
            good.setGoodAddress(resultSet.getString("goodAddress"));
            goods.add(good);
        }

        System.out.println(goods.toString());
        BaseDao.closeResource(null, preparedStatement, resultSet);
        return goods;
    }

    @Override
    public int UserGoodListDel(Connection connection, int goodId) throws SQLException {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        String sql = "delete from good where goodId = ?";
        Object []o = {goodId};
        int flag = BaseDao.excute(connection,preparedStatement,sql,o);
        System.out.println("good del中flag为："+flag);
        return flag;
    }

    @Override
    public int GoodAdd(Connection connection, int userId, String goodName, String goodAddress, String goodExplain) throws SQLException {
        int flag = 0;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        String sql = "insert into good(userId,goodName,goodAddress,goodExplain) values(?,?,?,?)";
        Object []o = {userId,goodName,goodAddress,goodExplain};
        if(connection != null){
            flag = BaseDao.excute(connection,preparedStatement,sql,o);
        }
        BaseDao.closeResource(null,null,null);
        return flag;
    }
}
